package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    /**
     * 更新床位
     */
    @Override
    public void updateBed(BedDto bedDto) {
        Bed bed = BeanUtil.toBean(bedDto, Bed.class);
        bed.setUpdateBy(1L);
        bed.setUpdateTime(LocalDateTime.now());
        if (ObjectUtil.isNotNull(bed.getBedStatus())){
            bed.setBedStatus(bed.getBedStatus());
        }
        bedMapper.updateBed(bed);
    }

    /**
     * 根据床位id删除床位
     * @param id 床位id
     */
    @Override
    public void deleteBed(long id) {
        bedMapper.deleteById(id);
    }

    /**
     * 根据id获取床位
     * @param id 床位ID
     */
    @Override
    public BedVo getBed(Long id) {
        Bed bed = bedMapper.getById(id);
        return BeanUtil.toBean(bed,BedVo.class);
    }

    /**
     * 新增床位
     */
    @Override
    public void insertBed(BedDto bedDto) {
        Bed bed = BeanUtil.toBean(bedDto, Bed.class);
        bed.setBedStatus(0);
        bed.setCreateBy(1L);
        bed.setCreateTime(LocalDateTime.now());
        try {
            bedMapper.insertBed(bed);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
    }

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

}

